import time
import unittest

from framework.common import util


class UtilTestCase(unittest.TestCase):
    # @unittest.skip("临时跳过")
    def test_fstring(self):
        r1 = util.fstring(["123"])
        self.assertEqual(r1, "123")

        r2 = util.fstring(["123", "456"])
        self.assertEqual(r2, "123456")

        r3 = util.fstring("123")
        self.assertEqual(r3, "123")

        r4 = util.fstring("\n123 ")
        self.assertEqual(r4, "123")

        r5 = util.fstring(["\n\n123"])
        self.assertEqual(r5, "123")

        r6 = util.fstring(["\n123 ", " 456\n"])
        self.assertEqual(r6, "123456")

        r7 = util.fstring(None)
        self.assertEqual(r7, "")

        r8 = util.fstring({})
        self.assertEqual(r8, "{}")
        pass

    def test_frange(self):
        r1 = util.frange([" 1", "2 "])
        self.assertEqual(r1, "1 - 2")

        r2 = util.frange(["1 "])
        self.assertEqual(r2, "1")

        r3 = util.frange([])
        self.assertEqual(r3, "")

        r4 = util.frange([" 1", "2 ", "3 "])
        self.assertEqual(r4, "1 - 2")

        r5 = util.frange(None)
        self.assertEqual(r5, "")
        pass

    def test_fnewlines(self):
        r1 = util.fnewlines(["\n", "\n", "\n\n\n   \n\n\n", "\n\n\n \n\n", "\n", "\n" "\n" "\n" "\n" "\n", "abc", "\n\n  ", "  \n", "\n\n\n", " \n\n", "\n", "\n", "\n"])
        self.assertEqual(r1, "abc")
        pass

    def test_fjson(self):
        json_str = """
        {"ajax":{"params":{"asinMetadataKeys":"adId:ParentReasonId:ParentReasonId.substitutions.purchase_date:rId","widgetTemplateClass":"PI::Similarities::ViewTemplates::Carousel::Desktop","linkGetParameters":"{\\\\"pd_rd_wg\\\\":\\\\"WM7qz\\\\",\\\\"pd_rd_r\\\\":\\\\"ec208e02-2188-41a4-8609-686f231977e5\\\\",\\\\"pf_rd_r\\\\":\\\\"61ZT93M1RVK0T8ARFPJQ\\\\",\\\\"pf_rd_p\\\\":\\\\"b65ee94e-1282-43fc-a8b1-8bf931f6dfab\\\\",\\\\"pd_rd_w\\\\":\\\\"wpqG5\\\\"}","productDetailsTemplateClass":"PI::P13N::ViewTemplates::ProductDetails::Desktop::DeliverySpeed","forceFreshWin":0,"painterId":"PersonalizationDesktopSimilaritiesCarousel","featureId":"SimilaritiesCarousel","reftagPrefix":"pd_sbs_86","imageHeight":160,"faceoutTemplateClass":"PI::P13N::ViewTemplates::Product::Desktop::CarouselFaceout","imageWidth":160,"auiDeviceType":"desktop","schemaVersion":2,"relatedRequestID":"61ZT93M1RVK0T8ARFPJQ","productDataFlavor":"FaceoutAddToCartShippingPromises","maxLineCount":6,"faceoutArgs":"{\\\\"productDetailsTemplateClass\\\\":\\\\"PI::P13N::ViewTemplates::ProductDetails::Desktop::DeliverySpeed\\\\"}"},"id_list":["B07B5YKH29::::","B0766LKN7W::::","B00AU6GB2K::::"],"url":"/gp/p13n-shared/faceout-partial","id_param_name":"asins"},"baseAsin":"B06ZXSDRWZ","name":"desktop-dp-sims_session-similarities","set_size":3}
        """
        r1 = util.fjson(json_str)
        self.assertEqual(len(r1["ajax"]["id_list"]), 3)

    def test_fjsonarray(self):
        r1 = util.fjsonarray(['{"ref":"pd_bxgy_1_1","price":149.99,"asin":"B07ZVXDY83"}', '{"ref":"pd_bxgy_2_2","price":19.95,"asin":"B0773FVRZY"}', '{"ref":"pd_bxgy_3_3","price":149.99,"asin":"B07ZRHF2PS"}'], "asin")
        self.assertEqual(len(r1), 3)

    def test_delta_days(self):
        t1 = time.strptime("2019-05-31 15:12:54", "%Y-%m-%d %H:%M:%S")
        t2 = time.strptime("2019-07-1 11:12:54", "%Y-%m-%d %H:%M:%S")
        dd1 = util.delta_days(t1, t2)
        self.assertEqual(dd1, 31)

        t2 = time.strptime("2020-03-31 9:56:06", "%Y-%m-%d %H:%M:%S")
        dd2 = util.delta_days(t1, t2)
        self.assertEqual(dd2, 305)

        dd3 = util.delta_days(t2, t1)
        self.assertEqual(dd3, 305)

        dd4 = util.delta_days(t2, None)
        self.assertIsNone(dd4)
        pass


if __name__ == '__main__':
    unittest.main()
